<?php
/**
 * 菜单管理
 */
class Controller_Admin_Menus extends Controller_Abstract
{

	function actionIndex()
	{
		// 分页信息
		$pagination = Q::ini('appini/pagination');
		$numPerPage = intval($this->_context->numPerPage)?intval($this->_context->numPerPage):intval($pagination['numPerPage']);
		$currentPage = intval($this->_context->pageNum)?intval($this->_context->pageNum):1;
		// 数据查询
		$parent_id = intval($this->_context->parentid);
		$data = Menus::find('parentid = ?',$parent_id)
					 ->limitPage($currentPage,$numPerPage)
					 ->get();
		$totle_count = Menus::find('parentid = ?',$parent_id)->getCount();
		// 数据加载
		$this->_view['pagination'] = array(
			'totalCount'=>$totle_count,
			'numPerPage'=>$numPerPage,
			'pageNumShown'=>$pagination['pageNumShown'],
			'currentPage'=>$currentPage
		);
		$this->_view['data'] = array(
			'fetch_data'=>$data,
			'context'=>$this->_context
		);
	}
	/**
	 * 
	 * 菜单添加
	 */
	function actionAdd()
	{
		$action = url('menus/add');
		$modify_id = intval($this->_context->mid);
		$return_array = array('statusCode'=>Helper_Dwz::DWZ_STATUS_ERROR,'message'=>'菜单添加失败!');// 服务器响应
		// 当操作为修改
		$menu_obj = false;
		if ($modify_id){
			$action .= '?mid='.$modify_id;
			$menu_obj = Menus::find($modify_id)->getOne();
			if (!$menu_obj->id())
			{
				return json_encode(array('statusCode' => Helper_Dwz::DWZ_STATUS_ERROR,'message'=>'您想要修改的信息没有找到!'));
			}
		}
		$add_form = new Form_Menu($action);
		if ($this->_context->isPOST())
		{
			// 参数校验
			if ($add_form->validate($_POST))
			{
				// 逻辑处理
				$form_values = $add_form->values();
				if ($modify_id && $menu_obj !== false){
					// 修改
					$menu_obj->changeProps($form_values);
				}else{
					// 添加
					$menu_obj = new Menus($form_values);
				}
				$menu_obj->save();
				$return_array = array('statusCode' => Helper_Dwz::DWZ_STATUS_OK,'message'=>'操作已成功!','callbackType'=>'closeCurrent','navTabId'=>'菜单管理');
			}else{
				// 参数校验返回
				if ($add_form->element('title')->isInvalid()){
					$return_array['message'] = $add_form->element('title')->errorMsg();
				}elseif($add_form->element('parentid')->isInvalid()){
					$return_array['message'] = $add_form->element('parentid')->errorMsg();
					
				}
			}
			// 返回数据
			return json_encode($return_array);
		}
		// 原始数据导入
		$add_form->element('parentid')->items = Menus::_get_menus_tree();
		// 默认数据导入
		if ($menu_obj !== false)
		{
			$add_form->import($menu_obj->toArray());
		}
		// 数据加载
		$this->_view['form'] = $add_form;
	}
	/**
	 * 
	 * 删除菜单(单一)
	 */
	function actionDelete()
	{
		$delete_id = intval($this->_context->did);
		$return_array = array('statusCode'=>Helper_Dwz::DWZ_STATUS_ERROR,'message'=>'菜单添加失败!');
		if ($delete_id){
			if (Menus::find('parentid = ?',$delete_id)->getCount())
			{
				// 有下级菜单不能删除
				$return_array['message'] = '此菜单下还有菜单信息,不能删除!';
			}else{
				// 可以删除
				Menus::meta()->deleteWhere('id = ?',$delete_id);
				$return_array = array('statusCode'=>Helper_Dwz::DWZ_STATUS_OK,'message'=>'菜单信息删除成功!');
			}
		}
		return json_encode($return_array);
	}
	/**
	 * 
	 * 主菜单显示
	 */
	function actionSidebar()
	{
		$menu_parent_id = intval($this->_context->mpid);
		$menu_obj = Menus::find('parentid = ? and isback and display',$menu_parent_id)->order('order asc')->getAll();
		
		$this->_view['power'] = unserialize($_SESSION['ADMIN_USER']['power']);
		$this->_view['menus'] = $menu_obj;
	}
}